package com.dingpeikeji.quickdeer.modules.messages.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.modules.messages.entity.TmpMessages;
import com.dingpeikeji.quickdeer.modules.messages.service.TmpMessagesService;

/**
 * 手工发送短信Controller
 * @author 123
 * @version 2017-05-23
 */
@Controller
@RequestMapping(value = "${adminPath}/messages/tmpMessages")
public class TmpMessagesController extends BaseController {

	@Autowired
	private TmpMessagesService tmpMessagesService;
	
	@ModelAttribute
	public TmpMessages get(@RequestParam(required=false) String id) {
		TmpMessages entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tmpMessagesService.get(id);
		}
		if (entity == null){
			entity = new TmpMessages();
		}
		return entity;
	}
	
	@RequiresPermissions("messages:tmpMessages:view")
	@RequestMapping(value = {"list", ""})
	public String list(TmpMessages tmpMessages, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TmpMessages> page = tmpMessagesService.findPage(new Page<TmpMessages>(request, response), tmpMessages); 
		model.addAttribute("page", page);
		return "modules/messages/tmpMessagesList";
	}

	@RequiresPermissions("messages:tmpMessages:view")
	@RequestMapping(value = "form")
	public String form(TmpMessages tmpMessages, Model model) {
		model.addAttribute("tmpMessages", tmpMessages);
		return "modules/messages/tmpMessagesForm";
	}

	@RequiresPermissions("messages:tmpMessages:edit")
	@RequestMapping(value = "save")
	public String save(TmpMessages tmpMessages, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tmpMessages)){
			return form(tmpMessages, model);
		}
		tmpMessagesService.save(tmpMessages);
		addMessage(redirectAttributes, "保存短信成功");
		return "redirect:"+Global.getAdminPath()+"/messages/tmpMessages/?repage";
	}
	
	@RequiresPermissions("messages:tmpMessages:edit")
	@RequestMapping(value = "delete")
	public String delete(TmpMessages tmpMessages, RedirectAttributes redirectAttributes) {
		tmpMessagesService.delete(tmpMessages);
		addMessage(redirectAttributes, "删除短信成功");
		return "redirect:"+Global.getAdminPath()+"/messages/tmpMessages/?repage";
	}

}